var express = require('express');
var router = express.Router();
const RestResult = require('../RestResult');
const log = require('../utils/logUtil').log();
const GroupEntity = require("../models/Group").GroupEntity
const UserEntity = require("../models/User").UserEntity;
//添加一个组
router.post('/addGroup', function (req, res, next) {
    log.debug(req.body);
    let GroupName = req.body.name;
    let resData = new RestResult();
    GroupEntity.findOne({name: GroupName}, function (err, doc) {
        let groupEntity = new GroupEntity({
            name: GroupName,
        });
        if (err) {
            resData.code = 1;
            resData.errorReason = RestResult.SERVER_EXCEPTION_ERROR_DESCRIPTION;
            res.send(resData);
        }
        else if (doc) {
            resData.code = 1;
            resData.errorReason = "组名已存在";
            res.send(resData);
        }
        else {
            groupEntity.save(function (err, group) {
                if (err) {
                    resData.code = 1;
                    resData.errorReason = RestResult.SERVER_EXCEPTION_ERROR_DESCRIPTION;
                    res.send(resData);
                }
                else {
                    resData.code = 0;
                    resData.returnValue = group;
                    res.send(resData);
                }
            })
        }

    })
})
//删除一个组
router.post('/deleteGroup', function (req, res, next) {
    log.debug(req.body);
    let _id = req.body._id;
    let resData = new RestResult();
    GroupEntity.remove({_id: _id}, function (err, doc) {
        if (err) {
            resData.code = 1;
            resData.errorReason = RestResult.SERVER_EXCEPTION_ERROR_DESCRIPTION;
            res.send(resData);
        }
        else {
            resData.code = 0;
            res.send(resData);
        }
    })
})
//获得所有组
router.post('/getGroup', function (req, res, next) {
    let resData = new RestResult;
    GroupEntity.find({}, function (err, docs) {
        if (err) {
            resData.code = 1;
            resData.errorReason = RestResult.SERVER_EXCEPTION_ERROR_DESCRIPTION;
            res.send(resData);
            return;
        }
        if (docs) {
            let result = [];
            for (let i = 0; i < docs.length; i++) {
                result[i] = {};
                result[i].name = docs[i].name;
                result[i]._id = docs[i]._id;
            }
            resData.code = 0;
            resData.returnValue = result;
            res.send(resData);
        }

    })
})
//更改组名
router.post('/updateGroup', function (req, res, next) {
    let resData = new RestResult;
    let GroupName = req.body.name;
    let _id = req.body._id;
    GroupEntity.findOne({name: GroupName}, function (err, doc) {
        if (err) {
            resData.code = 1;
            resData.errorReason = RestResult.SERVER_EXCEPTION_ERROR_DESCRIPTION;
            res.send(resData);
        }
        else if (doc) {
            resData.code = 1;
            resData.errorReason = "组名已存在";
            res.send(resData);
        }
        else {
            GroupEntity.findByIdAndUpdate(_id, {$set: {"name": GroupName}}, {new: true}, function (err, data) {
                if (err) {
                    resData.code = 1;
                    resData.errorReason = RestResult.SERVER_EXCEPTION_ERROR_DESCRIPTION;
                    res.send(resData);
                }
                else {
                    resData.code = 0;
                    log.debug(data);
                    resData.returnValue = data;
                    res.send(resData);
                }
            });
        }
    })
})
//添加组员
router.post('/addGroupMember', function (req, res, next) {
    log.debug(req.body);
    let groupName = req.body.gName;
    let groupId = req.body.gId;
    let memberName = req.body.mName;
    let memberId = req.body.mId;
    let resData = new RestResult();
    GroupEntity.findOne({_id: groupId}, function (err, doc) {
        if (err) {
            resData.code = 1;
            resData.errorReason = RestResult.SERVER_EXCEPTION_ERROR_DESCRIPTION;
            res.send(resData);
        }
        let member = doc._doc.member;
        for(let i in member){
            if(member[i].name == memberName) {
                resData.code = 1;
                resData.errorReason ="用户已在组中";
                res.send(resData);
                return;
            }
        }
        member.push({
            name: memberName,
            id: memberId
        });
        GroupEntity.findOneAndUpdate({_id: groupId},
            {
                $set: {
                    member: member
                }
            }, {new: true}, function (err, doc) {
                if (err) {
                    resData.code = 1;
                    resData.errorReason = RestResult.SERVER_EXCEPTION_ERROR_DESCRIPTION;
                    res.send(resData);
                }
                else {
                    UserEntity.findByIdAndUpdate(memberId, {$set: {"group": groupName}}, {new: true}, function (err, data) {
                        if (err) {
                            resData.code = 1;
                            resData.errorReason = RestResult.SERVER_EXCEPTION_ERROR_DESCRIPTION;
                            res.send(resData);
                        }
                    });
                    resData.code = 0;
                    resData.returnValue = doc;
                    res.send(resData);
                }
            })

    })
})
//获得组中所有成员
router.post('/getGroupMember', function (req, res, next) {
    let resData = new RestResult;
    let groupId = req.body.gId;
    GroupEntity.findOne({_id: groupId}, function (err, doc) {
        if (err) {
            resData.code = 1;
            resData.errorReason = RestResult.SERVER_EXCEPTION_ERROR_DESCRIPTION;
            res.send(resData);
        }
        else {
            if(doc){
                let member = doc._doc.member;
                resData.code = 0;
                resData.returnValue = member;
                res.send(resData);
            }
        }
    })
})
//删除组员
router.post('/deleteGroupMember', function (req, res, next) {
    log.debug(req.body);
    let groupName = req.body.gName;
    let groupId = req.body.gId;
    let memberName = req.body.mName;
    let memberId = req.body.mId;
    let resData = new RestResult();
    GroupEntity.findOne({_id: groupId}, function (err, doc) {
        let object ={
            name: memberName,
            id: memberId
        }
        if (err) {
            resData.code = 1;
            resData.errorReason = RestResult.SERVER_EXCEPTION_ERROR_DESCRIPTION;
            res.send(resData);
        }
        else{
            if(doc){
                let member = doc._doc.member;
                member.splice(member.indexOf(object), 1);
                GroupEntity.findOneAndUpdate({_id: groupId},
                    {
                        $set: {
                            member: member
                        }
                    }, {new: true}, function (err, doc) {
                        if (err) {
                            resData.code = 1;
                            resData.errorReason = RestResult.SERVER_EXCEPTION_ERROR_DESCRIPTION;
                            res.send(resData);
                        }
                        else {
                            UserEntity.findByIdAndUpdate(memberId, {$set: {"group": ""}}, {new: true}, function (err, data) {
                                if (err) {
                                    resData.code = 1;
                                    resData.errorReason = RestResult.SERVER_EXCEPTION_ERROR_DESCRIPTION;
                                    res.send(resData);
                                }
                            });
                            resData.code = 0;
                            resData.returnValue = doc;
                            res.send(resData);
                        }
                    })
            }else{
                resData.code = 1;
                resData.errorReason = "";
                res.send(resData);
            }

        }
    })
})
//获取非本组中所有人
router.post('/getNotGroupMember',function (req,res,data) {
    let resData = new RestResult;
    let groupId = req.body.gId;
    UserEntity.find({},function (err,docs) {
        if(err){
            resData.code = 1;
            resData.errorReason = RestResult.SERVER_EXCEPTION_ERROR_DESCRIPTION;
            res.send(resData);
            return ;
        }
        if(docs){
            let result =[];
            for(let i = 0;i<docs.length;i++){
                result[i] = {};
                result[i].name = docs[i].name;
                result[i]._id = docs[i]._id;
                log.debug(docs[i]);
            }
            GroupEntity.findOne({_id: groupId}, function (err, doc) {
                let member = doc._doc.member;
                if (err) {
                    resData.code = 1;
                    resData.errorReason = RestResult.SERVER_EXCEPTION_ERROR_DESCRIPTION;
                    res.send(resData);
                }
                let returnValue = [];
                for(let i in result){
                    let flag = 1;
                    for(let j in member){
                        if(result[i].name == member[j].name){
                            flag = 0;
                            break;
                        }
                    }
                    if(flag){
                        returnValue.push(result[i]);
                    }
                }
                resData.code = 0;
                resData.returnValue = returnValue;
                res.send(resData);
            })
        }
    })
})

router.post('/getNotGroupMemberButAdmin',function (req,res,data) {
    let resData = new RestResult;
    let groupId = req.body.gId;
    UserEntity.find({},function (err,docs) {
        if(err){
            resData.code = 1;
            resData.errorReason = RestResult.SERVER_EXCEPTION_ERROR_DESCRIPTION;
            res.send(resData);
            return ;
        }
        if(docs){
            let result =[];
            for(let i = 0;i<docs.length;i++){
                result[i] = {};
                result[i].name = docs[i].name;
                result[i]._id = docs[i]._id;
                result[i].isAdmin=docs[i].isAdmin;

            }
            GroupEntity.findOne({_id: groupId}, function (err, doc) {
                let member = doc._doc.member;
                if (err) {
                    resData.code = 1;
                    resData.errorReason = RestResult.SERVER_EXCEPTION_ERROR_DESCRIPTION;
                    res.send(resData);
                }
                let returnValue = [];
                for(let i in result){
                    let flag = 1;
                    for(let j in member){
                        if(result[i].name == member[j].name){
                            flag = 0;
                            break;
                        }
                    }
                    if(flag&&result[i].isAdmin==false){
                        returnValue.push(result[i]);
                    }
                }
                resData.code = 0;
                resData.returnValue = returnValue;
                res.send(resData);
            })
        }
    })
})

module.exports = router;